package com.example.module_detail

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.entity.MovieVideoEntity
import com.example.base.RouterPath
import com.example.common.event.LiveDataBus
import com.example.common.event.LiveDataBusEvent
import com.example.common.mvi.BaseMviFragment
import com.example.detail.R
import com.example.detail.databinding.FragmentIntroduceBinding
import com.example.image.ImgOptions
import com.example.image.LoadImageUtil
import com.example.module_detail.adapter.DetailMoviePagingAdapter
import com.example.module_detail.entity.AuthorEntity
import com.example.module_detail.intent.DetailViewIntent
import com.example.module_detail.state.DetailUiState
import com.example.module_detail.vm.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RouterPath.PATH_FRAGMENT_DETAIL_INTRODUCE)
class IntroduceFragment : BaseMviFragment<FragmentIntroduceBinding>() {


    private val mViewModel :DetailViewModel by activityViewModels()

    private lateinit var mPagindDetailAdapter : DetailMoviePagingAdapter


    @Autowired
    @JvmField
    var userId:String = ""

    @Autowired
    @JvmField
    var videoTypeId:Int = -1


    override fun initViews() {
        ARouter.getInstance().inject(this)

        mPagindDetailAdapter = DetailMoviePagingAdapter()


        mPagindDetailAdapter.setItemClickListener(object:DetailMoviePagingAdapter.ItemClickListener{
            override fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity) {
                LiveDataBus.with<LiveDataBusEvent>("movie_detail_options").value = LiveDataBusEvent(1, entity)
            }
        })
        mDataBinding.rvUserVideo.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
        mDataBinding.rvUserVideo.adapter = mPagindDetailAdapter

        loadVideoByTwo()
    }
    private fun loadVideoByTwo(){
        lifecycleScope.launch {

            if (videoTypeId == -1){
                mViewModel.loadRecommendVideo().collectLatest {
                    mPagindDetailAdapter.submitData(it)
                }
            }else{
                mViewModel.loadMutilVideoById(videoTypeId).collectLatest {
                    mPagindDetailAdapter.submitData(it)
                }
            }
        }
    }

    override fun getLayoutId(): Int =R.layout.fragment_introduce


    override suspend fun sendViewIntent() {
        mViewModel.channel.send(DetailViewIntent.LoadAuthorInfo(userId))
    }

    override suspend fun collectUiState() {
        mViewModel.uiState.collect {
            when (it) {

                is DetailUiState.LoadAuthorInfoSuccess -> setAuthroInfo(it.entity)
                else->{}
            }
        }
    }

    private fun setAuthroInfo(entity:AuthorEntity){
        LoadImageUtil.loadUrl(requireContext(), entity.avatar_url,  mDataBinding.ivUserIcon, ImgOptions.Builder.isCircle(true).build())
        mDataBinding.tvUserName.text = entity.name
        mDataBinding.tvUserDesc.text = entity.author_desc
        mDataBinding.tvLikeCount.text = entity.followerscount
        mDataBinding.tvSendTime.text = entity.ctime
    }


}